tree.using 'tree.ui'
tree.using 'tree.ressources'

tree.ui.Window.set_title strings.get_string 'title'

hbox = new tree.ui.HBox


button1 = new tree.ui.Button 'button1'
button2 = new tree.ui.Button 'button2'
button3 = new tree.ui.Button 'button3'

left = new tree.ui.VBox
left.add button1
left.add button2
left.add button3

left.set_style 'width', 'auto'
left.set_style 'height', 'auto'

hbox.add left

center = new tree.ui.Table

hbox.add center

center.set_style 'width', 'auto'
center.set_style 'height', 'auto'

proxy = new tree.io.rpc.JSON_Proxy '/rpc'
plugins = proxy.call_method 'list_bundles', []

offset = 0
for plugin in plugins
    console.log offset
    label = new tree.ui.Label plugin
    
    b1 = new tree.ui.Button strings.get_string 'show_deps'
    
    local_offset = offset
    local_plugin = plugin
    clicked = (widget, event, args) ->
        if widget.treeview?
            widget.set_text strings.get_string 'show_deps'
            center.remove widget.treeview
            widget.treeview = null
            return
        widget.set_text strings.get_string 'hide_deps'
        [local_offset, local_plugin] = args
        widget.treeview = new tree.ui.TreeView
        
        build_graph = (plugin) ->
            ret = {}
            deps = proxy.call_method 'get_dependencies', [plugin]
            for dep in deps
                [type, name] = dep
                val = null
                if type == 'bundle'
                    val = build_graph name
                    if (Object.keys(val)).length == 0
                        val = null
                ret[type + ': ' + name] = val
            ret
        
        graph = build_graph local_plugin 
        
        widget.treeview.set_data graph
        
        center.add widget.treeview, 3, local_offset
    
    b1.connect 'clicked', clicked, [local_offset, local_plugin]
    
    text = ''
    active = proxy.call_method 'is_bundle_active', [plugin]
    
    if active is true
        text = 'Stop'
    else
        text = 'Activate'
    
    b2 = new tree.ui.Button text
    
    center.add label, 0, offset
    center.add b1, 1, offset
    center.add b2, 2, offset
    offset += 1
    
tree.ui.Window.show hbox
